﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Text.RegularExpressions;

namespace Diplomova_prace
{
    /// <summary>
    /// Třída implementující XML parser, využívá se pro parsování cvičných úloh do aplikace
    /// </summary>
    public class XMLParser
    {
        int RowId = -1;
        int CellId = -1;
        List<string> listEl = new List<string>();
        Dictionary<string, string> dict = new Dictionary<string, string>();
        StringBuilder sb = new StringBuilder();
        public Dictionary<string, string> ParserValOfEl(string PathToXml,XmlNode node = null)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(PathToXml);
            XmlNode root = doc.DocumentElement;
            PassNodes(root); 
            return dict;
        }
        
        private void PassNodes(XmlNode Node)
        {

            foreach (XmlNode subNode in Node.ChildNodes)
            {
                if (subNode.Name == "Row")
                {
                    RowId++;
                    CellId = -1;
                }
                if (Node.Name == "Cell" && subNode.Name == "#text")
                {
                    CellId++;
                }
                if (Node.Name == "uloha")
                {
                    RowId = -1;
                    CellId = -1;
                }

                if (subNode != null && subNode.Value != null)
                {
                    if (Node.Name == "Cell")
                        dict.Add(Node.ParentNode.ParentNode.Name +" ID:"+RowId + ","+CellId, subNode.Value);
                    else
                        dict.Add(Node.Name, subNode.Value);
                }
               // string a = Node.Name;
                PassNodes(subNode);
            }
        }
        public string CheckByXSD(string PathToXSD,string PathToXml)
        {
            XmlReaderSettings booksSettings = new XmlReaderSettings();
            booksSettings.Schemas.Add(null, PathToXSD);
            booksSettings.ValidationType = ValidationType.Schema;
            booksSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler);
            XmlReader books = XmlReader.Create(PathToXml, booksSettings);
            try
            {
                while (books.Read()) { }
            }
            catch (Exception e)
            {
                sb.Clear();
                sb.Append(e.Message);
            }
            books.Close();
            return sb.ToString();
        }
        void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
        {
            
            if (e.Severity == XmlSeverityType.Warning)
            {
                sb.Append("Varování: " + e.Message+"\n");
            }
            else if (e.Severity == XmlSeverityType.Error)
            {
                sb.Append("Chyba: " + e.Message+"\n");
            }
        }
    }
}
